package com.kaifamiao.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author lucien-ma
 * @date 2021/8/27
 * @apiNote
 */
public class ActorJDKProxy {

    private static IActor actor = new Actor("李连杰");

    public static void show() {
        Object obj = Proxy.newProxyInstance(ActorJDKProxy.class.getClassLoader(),
                actor.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("jdk 代理模式");
                        Object result = null;
                        if ("basic".equals(method.getName())) {
                            System.out.println("普通表演抽取 30% 的钱");
                            result = method.invoke(actor, args);
                        } else if ("danger".equals(method.getName())) {
                            System.out.println("危险表演抽取 20% 的钱");
                            result = method.invoke(actor, args);
                        } else {
                            result = method.invoke(actor, args);
                        }
                        return result;
                    }
                });
        IActor a1 = (IActor) obj;
        a1.danger(900.0);
    }

    public static void main(String[] args) {
        show();
    }

}
